package net.entelijan.cobean.examples.showcase.config.text;

import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.testutil.CobeanTestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JTextComponentFixture;
import org.junit.Assert;
import org.junit.Test;

public class TextMultiFailureFestTestCase {

	@SuppressWarnings("unused")
	private static Log log = LogFactory
			.getLog(TextMultiFailureFestTestCase.class);

	@Test
	public void testInit() {
		ConfigMultiFailureTextPanel panel = new ConfigMultiFailureTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigMultiFailureTextShowcase sc = new ConfigMultiFailureTextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			JTextComponentFixture authorNameTextBox = frameFix
					.textBox("authorNameTextField");
			JTextComponentFixture authorBirthdayTextBox = frameFix
					.textBox("authorBirthdayTextField");
			JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			JTextComponentFixture yearFailureTextBox = frameFix
					.textBox("yearFailureTextField");
			JTextComponentFixture authorBirthdayFailureTextBox = frameFix
					.textBox("authorBirthdayFailureTextField");
			sc.init(panel);
			pause(100);

			authorNameTextBox.requireText("V.S. Naipaul");
			authorBirthdayTextBox.requireText("1932-08-17");
			yearTextBox.requireText("1961");
			yearFailureTextBox.requireText("");
			authorBirthdayFailureTextBox.requireText("");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetInvalidYearDiffValue() {
		ConfigMultiFailureTextPanel panel = new ConfigMultiFailureTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigMultiFailureTextShowcase sc = new ConfigMultiFailureTextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			JTextComponentFixture authorNameTextBox = frameFix
					.textBox("authorNameTextField");
			JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			JTextComponentFixture yearFailureTextBox = frameFix
					.textBox("yearFailureTextField");
			sc.init(panel);
			pause(100);

			yearTextBox.deleteText();
			yearTextBox.setText("aaaaa");
			authorNameTextBox.focus();
			pause(100);

			Assert.assertEquals(1961, sc.getBook().getYear().intValue());
			yearFailureTextBox.requireText("'aaaaa' is not a valid integer");

			sc.getBook().setYear(1999);
			pause(100);

			yearTextBox.requireText("1999");
			yearFailureTextBox.requireText("");
		} finally {
			frameFix.cleanUp();
		}

	}

	// TODO Check how this testcase could be passed
	// @Test
	public void testSetInvalidYearSameValue() {
		ConfigMultiFailureTextPanel panel = new ConfigMultiFailureTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigMultiFailureTextShowcase sc = new ConfigMultiFailureTextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			JTextComponentFixture authorNameTextBox = frameFix
					.textBox("authorNameTextField");
			JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			JTextComponentFixture yearFailureTextBox = frameFix
					.textBox("yearFailureTextField");
			sc.init(panel);
			pause(100);

			yearTextBox.deleteText();
			yearTextBox.setText("aaaaa");
			authorNameTextBox.focus();
			pause(100);

			Assert.assertEquals(1961, sc.getBook().getYear().intValue());
			yearFailureTextBox.requireText("'aaaaa' is not a valid integer");

			// Setting the same value as it was before the invalid 'aaa'
			sc.getBook().setYear(1961);
			pause(100);

			yearTextBox.requireText("1961");
			yearFailureTextBox.requireText("");
		} finally {
			frameFix.cleanUp();
		}

	}

	private synchronized void pause(int timeMs) {
		try {
			wait(timeMs);
		} catch (InterruptedException e) {
			// Do not mind
		}

	}

}
